package com.pphh.demo;

/**
 * A simple demo to load groovy class dynamically
 *
 * @author huangyinhuang
 * @date 9/25/2018
 */

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;
import java.net.URL;

public class SimpleDynamicGroovy {

    public static void main(String[] args) throws Exception {
        URL url = SimpleDynamicGroovy.class.getClassLoader().getResource("simple.groovy");
        File file = new File(url.getFile());
        ClassLoader parent = ClassLoader.getSystemClassLoader();
        GroovyClassLoader loader = new GroovyClassLoader(parent);
        Class groovyClass = loader.parseClass(file);
        GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
        Object[] param = {123, 321};
        int result = (int) groovyObject.invokeMethod("add", param);
        System.out.println("result = " + result);
    }

}
